//index.js
//获取应用实例
const app = getApp()
var Constant = require('../../utils/constant.js');
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    //跳转到log界面
    // wx.navigateTo({
    //   url: '../logs/logs'
    // })
  },
  onLoad: function () {
    
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      console.log(app.globalData.openid)
      //getOpenId(res);
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        wx.login({
          success: function (r) {
            var code = r.code;//登录凭证
            var res = { 'res': { 'encryptedData': encryptedData, 'iv': iv } }
            getOpenId(res, code)
          }
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          wx.login({
            success: function (r) {
              var code = r.code;//登录凭证
              var res = { 'res': { 'encryptedData': encryptedData, 'iv': iv } }
              getOpenId(res, code)
            }
          })
        }
      })
    }
  },

  getUserInfo: function(e) {
    console.log(e)
    var encryptedData = e.detail.encryptedData; 
    var iv = e.detail.iv;
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    // wx.login({
    //   success: function (r) {
    //     var code = r.code;//登录凭证
    //     var res = { 'res':{'encryptedData': encryptedData, 'iv': iv}}
    //     getOpenId(res, code)
    //     }
    //   })
    wx.reLaunch({
      url: '../menu/menu',
    })
    
  },
  myAppleyFor: function(e){
    if (checkStatus()) {
      //检查是否为亲属
      if (Constant.STATE_USER == Constant.STATE_USER_ISRELATIVES) {
        wx.showModal({
          title: '',
          content: '亲属无法查看该信息',
          showCancel: false
        })
        return false;
      }
      wx.navigateTo({
        url: '../registration/longvisitorhistory'
      })
    }
  },
  myVisitor: function(e){
    if (checkStatus()) {
      wx.navigateTo({
        url: '../takephoto/visitorpicture'
      })
    }
  },
  myCount: function(e){
    if (checkStatus()) {
      var userTypeName;
      console.log("aaaa", app.globalData.userType)
      if (app.globalData.userType == 1){
        userTypeName = "业主";
      }else{
        userTypeName = "业主亲属";
      }

      wx.navigateTo({
        url: '../confirm/confirm?personname=' + app.globalData.username + "&personidcardno=" + app.globalData.idcard + "&personphoneno=" + app.globalData.phonenumber + "&persondoorno=" + '' + "&unitno=" + '' + "&persontype=" + app.globalData.userType + "&sex=" + '' + "&cardType=" + '居民身份证' + "&wordType=" + '' + "&role=" + userTypeName + "&location=" + app.globalData.unitno + "&community=" + '' + "&buildNum=" + '' + "&photopath=" + app.globalData.personpic + "&isUserInfo=" + true,
      })
    }
  },

  about: function(e){
    wx.navigateTo({
      url: '../about/about'
    })
  },
  feedback: function(e){
    if (app.globalData.openid == ''){
      wx.showToast({
        title: '登录之后才能进行反馈',
        icon: 'none'
      })
      return;
    }
    wx.navigateTo({
      url: '../feedback/feedback',
    })
  }
})

function checkStatus() {
  if (Constant.STATE_USER == Constant.STATE_USER_NONE) {
    wx.showModal({
      title: '',
      content: '用户未注册，暂时无法查看',
      showCancel: false
    })
    return false;
  }else  if (Constant.STATE_USER == Constant.STATE_OPENID_ERROR) {
    wx.showModal({
      title: '',
      content: '系统错误，暂时无法查看',
      showCancel: false
    })
    return false;
  }
  else if (Constant.STATE_USER != Constant.STATE_USER_VERIFYED) {
    wx.showModal({
      title: '',
      content: '用户还在审核中，暂时无法查看',
      showCancel: false
    })
    return false;
  } 
  return true;
}

function getOpenId(res,code){
  //console.log({ encryptedData: res.encryptedData, iv: res.iv, code: code })
  //3.请求自己的服务器，解密用户信息 获取unionId等加密信息
  wx.request({
    url: Constant.BASEURL + Constant.USER_LOGIN_URL + '&encryptedData=' + res.encryptedData
      + '&iv=' + res.iv + '&code=' + code,//自己的服务接口地址
    method: 'GET',
    // header: {
    //   'content-type': 'application/x-www-form-urlencoded'
    // },
    // data: { encryptedData: res.encryptedData, iv: res.iv, code: code },
    success: function (res) {
      console.log(res.data)
      app.globalData.openid = res.data.openId;
      
      //4.解密成功后 获取自己服务器返回的结果
      if (res.data.status == 1) {
        var result = res.data.msg;
        console.log(result)
      } else {
        console.log('解密失败')
      }

    },
    fail: function () {
      console.log('系统错误')
    }
  })
}
